import requests
from bs4 import BeautifulSoup
import copy
from io import BytesIO
import pandas


def check_email(email_list):
    """
    邮箱地址有效性校验。数据来自第三方爬虫所得，不能用于生产。
    :param email_list: 邮箱地址列表
    :return: dict
    """
    email_pool = "\n".join(email_list)
    res = requests.post("http://email.qhyt1688.com/Home/Vemail", data={"emailpool": email_pool}).text
    bs = BeautifulSoup(res, "html5lib")

    result = {}
    for i in bs.find_all("textarea")[0].text.split("\n"):
        if "=" not in i and i.strip():
            result_split = i.split("\t")
            if "真实存在" in result_split[-1]:
                status = "1"
            elif "不存在" in result_split[-1]:
                status = "0"
            else:
                status = "-1"
            result[result_split[0]] = status

    return result


def to_valid_dict(source_dict, invalid=("", {}, None, [], ())):
    """
    将原始数据字典中value等于invalid中的某一个的key删除掉
    :param source_dict: 原始数据字典
    :param invalid: 无效值列表
    :return: None
    """
    temp = copy.copy(source_dict)
    for k, v in temp.items():
        if isinstance(temp[k], dict):
            to_valid_dict(source_dict[k], invalid)
        elif v in invalid:
            source_dict.pop(k)

    # 去除上边删掉的空值之后，会剩余父节点为{}的情况，进行处理。
    temp = copy.copy(source_dict)
    for k, v in temp.items():
        if v == {}:
            source_dict.pop(k)


def to_short_json_for_list(source_dict, max_len=1):
    """
    将字典中的列表进行长度裁剪，裁剪为只有max_len的长度。并新增对应列表的key_len的字段，值为原始字典的值。
    可用作业务中的日志打印。这样能避免数据中有大列表的情况造成的日志过大，而很多时候其实打印数据结构即可。
    :param source_dict: 原始字段
    :param max_len: 数据中的列表裁剪后剩余的长度
    :return: 返回处理后的原数数据字段
    """
    temp = copy.copy(source_dict)
    for k, v in temp.items():
        if isinstance(temp[k], dict):
            to_short_json_for_list(source_dict[k])
        elif isinstance(v, list):
            source_dict["_%s_len" % k] = len(v)
            source_dict[k] = v[:max_len]


def trans_record_data_to_io(data_list, rename_dict, col_order):
    """
    将列表转换为excel的io对象
    :param data_list: 数据列表
    :param rename_dict:
    :param col_order:
    :return:
    """
    file = BytesIO()
    df = pandas.DataFrame(data_list)
    df.rename(columns=rename_dict, inplace=True)
    writer = pandas.ExcelWriter(file, engine='xlsxwriter')
    df.to_excel(writer, index=False, columns=col_order)
    writer.save()
    return file.getvalue()
